<template>
  <main class="pay">
    <section class="pay-con">
      <div class="con-site">
        <h4>收获信息:</h4>
        <ul>
          <li>姓名：{{ addressee.name }}</li>
          <li>电话：{{ addressee.phone }}</li>
          <li>收获地址：{{ addressee.address }}</li>
        </ul>
      </div>
      <div class="con-list">
        <ul>
          <li v-for="item in payList" :key="item.id">
            <div>
              <img :src="host + item.image" alt="" />
            </div>
            <div>
              <h4>{{ item.name }}</h4>
              <p>￥{{ item.price }}</p>
            </div>
            <span>X{{ item.amount }}</span>
          </li>
        </ul>
      </div>
      <div class="con-method">
        <label
          ><input
            type="radio"
            value="微信"
            name="pay"
            v-model="payData.pay_type"
          />微信</label
        >
        <label
          ><input
            type="radio"
            value="支付宝"
            name="pay"
            v-model="payData.pay_type"
          />支付宝</label
        >
      </div>
      <p class="con-pay">
        总金额：{{ total }}
        <a href="javascript:;" @click="pay">确认支付</a>
      </p>
    </section>
  </main>
</template>

<script>
import { mapState } from "vuex";
import config from "../../config";
import { addOrderApi } from "../../api";
export default {
  name: "Pay",
  data() {
    return {
      host: config.host,
      payData: {
        pay_type: "微信",
        goodsList: [],
      },
    };
  },
  computed: {
    ...mapState("user", ["addressee"]),
    ...mapState("pay", ["payList"]),
    total() {
      if (this.payList.length === 0) return 0;
      return this.payList.reduce((acc, curr) => {
        return (acc += curr.amount * curr.price);
      }, 0);
    },
  },
  watch: {
    payList: {
      immediate: true,
      deep: true,
      handler(newValue) {
        if (newValue.length <= 0) return;
        let goodsList = newValue.reduce((acc, curr) => {
          acc.push({ goods_id: curr.goods_id, amount: curr.amount });
          return acc;
        }, []);
        this.payData.goodsList = goodsList;
      },
    },
  },
  methods: {
    pay() {
      if (!confirm("是否确认支付！")) return;
      addOrderApi(
        (response) => {
          console.log(response);
          if (response.data.status === 1) {
            this.$bus.$emit("showTips", "支付成功！");

            // 重新获取购物车长度
            this.$store.dispatch("user/getCartLen");
            // 跳转到订单页
            this.$router.push("/indent");
          }
        },
        (err) => {
          console.log(err);
        },
        this.payData
      );
    },
  },
  mounted() {
    setTimeout(() => {
      if (this.payData.goodsList.length <= 0) {
        this.$router.push("/shoppingCart");
      }
    }, 300);
  },
};
</script>

<style lang="stylus" scoped>
.pay {
  width: 100%;
  min-width: 800px;
  min-height: 400px;
  margin-top: 60px;
  background-color: #f8f8f8;

  .pay-con {
    width: 80%;
    min-width: 800px;
    min-height: 300px;
    margin: 0 auto;
    padding: 20px 0 30px;
  }

  .con-list {
    padding: 20px 0;
    position: relative;

    li {
      display: flex;
      padding: 6px;
      background-color: #fff;
      border-radius: 4px;
      margin-top: 20px;
      overflow: hidden;
      color: #999;

      img {
        width: 70px;
      }

      div:nth-of-type(2) {
        padding-left: 20px;

        h4 {
          font-size: 18px;
        }

        p {
          color: #fe4d1b;
        }
      }

      span {
        line-height: 70px;
        position: absolute;
        right: 30px;
        font-size: 16px;
      }
    }
  }

  .con-site {
    padding: 10px;
    font-size: 12px;
    border-radius: 4px;
    background-color: #fff;
    box-shadow: 0 0 6px 1px rgba(0, 0, 0, 0.3);
    color: #999;

    h4 {
      color: #000;
      padding-left: 4px;
      border-left: 4px solid #fe4d1b;
    }
  }

  .con-method {
    padding: 50px 0;
    text-align: center;

    label {
      padding: 6px 10px;
      border: 1px solid #999;
      border-radius: 4px;
      background-color: #fff;
      margin-right: 10px;
    }
  }

  .con-pay {
    text-align: center;

    a {
      text-decoration: none;
      padding: 4px 30px;
      color: #fff;
      background-color: #fe4d1b;
      margin: 0 10px 0 20px;
    }
  }
}
</style>